package com.example.haxballandroid;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.support.v4.app.NavUtils;

public class CreateActivity extends Activity {
	
	EditText gameName;
	EditText password;
	EditText score;
	EditText time;
	String name, pass;
	public final static String CREATE = "com.example.haxballandroid.MESSAGE";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create);
		
		gameName = (EditText)findViewById(R.id.create_game_name);
		password = (EditText)findViewById(R.id.create_game_password);
		score = (EditText)findViewById(R.id.create_score_limit);
		time = (EditText)findViewById(R.id.create_time_limit);
		
	}
	
	public BroadcastReceiver broadcastReceiver = new BroadcastReceiver(){
		
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			// TODO Auto-generated method stub
			Log.d("Create Activity", "Received Update");
			if(!ActionUpdate.createStatus){
				createUnsuccessfulDialog();
			}
			else{
				Log.d("Game Created", "Received");
				if(ActionUpdate.receiveGameUpdate){
					startTeamActivity();
				}
			}
			
		}

	};
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_create, menu);
		return true;
	}
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	/** Called when the user clicks the Send button */
	public void createField(View view){
		//Do something in response to button
		name = gameName.getText().toString();
		pass = password.getText().toString();
		int timeLimit = 5;
		int scoreLimit = 5;
		if(time.getText().toString().matches("")){
			timeLimit = 5;
		}
		else{
			try{
				timeLimit = Integer.parseInt(time.getText().toString());
			}
			catch (NumberFormatException nfe){
				createTimeErrorDialog();
			}
		}
		
		if(score.getText().toString().matches("")){
			scoreLimit = 5;
		}
		else{
			try{
				scoreLimit = Integer.parseInt(score.getText().toString());
			}
			catch (NumberFormatException nfe){
				createScoreErrorDialog();
			}
		}
		
		
		if(!(name.matches(""))){
			Intent intentAct = new Intent(this, TeamActivity.class);
			Intent intentServ = new Intent(this, ActionUpdate.class);
			intentServ.putExtra(CREATE, "create");
			intentServ.putExtra("GameName", name);
			intentServ.putExtra("Password", pass);
			intentServ.putExtra("TimeLimit", timeLimit);
			intentServ.putExtra("ScoreLimit", scoreLimit);
			startService(intentServ);
			startActivity(intentAct);
			
		}
		else{
			this.createErrorDialog(view);
		}
		
	}
	
	public void startTeamActivity(){
		Intent intentAct = new Intent(this, TeamActivity.class);
		Intent intentServ = new Intent(this, ActionUpdate.class);
		startService(intentServ);
		startActivity(intentAct);
	}
	
	
	public void createTimeErrorDialog(){
		
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Error");
		alertDialog.setMessage("The Time Limit you have entered is not valid");
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			dialog.cancel();
		}
		});
		alertDialog.show();
		
	}
	
	public void createScoreErrorDialog(){
		
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Error");
		alertDialog.setMessage("The Score Limit you have entered is not valid");
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			dialog.cancel();
		}
		});
		alertDialog.show();
		
	}
	
	public void createErrorDialog(View view){
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Error");
		alertDialog.setMessage("Please enter a game name");
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			dialog.cancel();
		}
		});
		alertDialog.show();
	}
	
	public void createUnsuccessfulDialog(){
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Error");
		alertDialog.setMessage("The game could not be created, Please Try again");
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			dialog.cancel();
		}
		});
		alertDialog.show();
	} 
	

}
